﻿package com.sh.game.util
{
    import flash.display.*;
    import flash.geom.*;

    public class BitmapScale9Drawer extends Object
    {
        private var _bitmapdata:BitmapData;
        private var _width:int;
        private var _height:int;
        private var _scale9Grid:Rectangle;
        private var _cutRect:Rectangle;
        private var _scale9GridRect:Array;
        private var _x:int = 0;
        private var _y:int = 0;
        public static var _defaultBitmapData_:BitmapData = new BitmapData(10, 10, false, 16777215);

        public function BitmapScale9Drawer(param1:BitmapData = null, param2:Rectangle = null)
        {
            this._bitmapdata = param1;
            if (this._bitmapdata == null)
            {
                this._bitmapdata = _defaultBitmapData_;
            }
            this._scale9Grid = param2;
            if (this._scale9Grid == null)
            {
                this._scale9Grid = new Rectangle(80, 30, 121, 340);
            }
            this.updataCutRectangle();
            return;
        }// end function

        public function set x(param1:int) : void
        {
            this._x = param1;
            this.updataCutRectangle();
            return;
        }// end function

        public function set y(param1:int) : void
        {
            this._y = param1;
            this.updataCutRectangle();
            return;
        }// end function

        public function setPos(param1:int, param2:int) : void
        {
            this._x = param1;
            this._y = param2;
            this.updataCutRectangle();
            return;
        }// end function

        public function get curRect() : Rectangle
        {
            return this._cutRect;
        }// end function

        public function reander() : void
        {
            this.updataCutRectangle();
            return;
        }// end function

        private function updataCutRectangle() : void
        {
            var _loc_13:int = 0;
            var _loc_14:Matrix = null;
            var _loc_15:Number = NaN;
            var _loc_16:Number = NaN;
            if (this._bitmapdata == null || this._bitmapdata.width == 0 && this._bitmapdata.height == 0)
            {
                return;
            }
            var _loc_1:* = this._bitmapdata.rect;
            var _loc_2:* = this._width - this._scale9Grid.x - (_loc_1.right - this._scale9Grid.right);
            var _loc_3:* = this._height - this._scale9Grid.y - (_loc_1.bottom - this._scale9Grid.bottom);
            if (_loc_2 < 0)
            {
                _loc_2 = 0;
                ;
            }
            if (_loc_3 < 0)
            {
                _loc_3 = 0;
                ;
            }
            this._cutRect = new Rectangle(this._scale9Grid.x, this._scale9Grid.y, _loc_2, _loc_3);
            var _loc_4:Array = [0, this._scale9Grid.x, this._scale9Grid.right];
            var _loc_5:Array = [0, this._scale9Grid.y, this._scale9Grid.bottom];
            var _loc_6:Array = [this._scale9Grid.x, this._scale9Grid.width, _loc_1.right - this._scale9Grid.right];
            var _loc_7:Array = [this._scale9Grid.y, this._scale9Grid.height, _loc_1.bottom - this._scale9Grid.bottom];
            var _loc_8:Array = [0, this._cutRect.x, this._cutRect.right];
            var _loc_9:Array = [0, this._cutRect.y, this._cutRect.bottom];
            var _loc_10:Array = [this._cutRect.x, this._cutRect.width, _loc_1.right - this._scale9Grid.right];
            var _loc_11:Array = [this._cutRect.y, this._cutRect.height, _loc_1.bottom - this._scale9Grid.bottom];
            this._scale9GridRect = new Array();
            var _loc_12:int = 0;
            while (_loc_12 < 3)
            {
                
                _loc_13 = 0;
                while (_loc_13 < 3)
                {
                    
                    _loc_14 = new Matrix();
                    _loc_15 = _loc_10[_loc_12] / _loc_6[_loc_12];
                    _loc_16 = _loc_11[_loc_13] / _loc_7[_loc_13];
                    _loc_14.scale(_loc_15, _loc_16);
                    _loc_14.ty = _loc_9[_loc_13] - _loc_5[_loc_13] + this._y;
                    _loc_14.tx = _loc_8[_loc_12] - _loc_4[_loc_12] + this._x;
                    if (_loc_12 == 1)
                    {
                        _loc_14.tx = (1 - _loc_15) * _loc_8[1] + this._x;
                    }
                    if (_loc_13 == 1)
                    {
                        _loc_14.ty = (1 - _loc_16) * _loc_9[1] + this._y;
                    }
                    this._scale9GridRect.push({rect:new Rectangle(_loc_8[_loc_12], _loc_9[_loc_13], _loc_10[_loc_12], _loc_11[_loc_13]), matrix:_loc_14});
                    _loc_13++;
                }
                _loc_12++;
            }
            return;
        }// end function

        public function set width(param1:Number) : void
        {
            this._width = param1;
            this.updataCutRectangle();
            return;
        }// end function

        public function set height(param1:Number) : void
        {
            this._height = param1;
            this.updataCutRectangle();
            return;
        }// end function

        public function setup(param1:BitmapData, param2:Rectangle) : void
        {
            this.bitmapData = param1;
            this.scale9Grid = param2;
            return;
        }// end function

        public function set scale9Grid(param1:Rectangle) : void
        {
            this._scale9Grid = param1;
            this.updataCutRectangle();
            return;
        }// end function

        public function get scale9Grid() : Rectangle
        {
            return this._scale9Grid;
        }// end function

        public function set rect(param1:Rectangle) : void
        {
            this._x = param1.x;
            this._y = param1.y;
            this._width = param1.width;
            this._height = param1.height;
            this.updataCutRectangle();
            return;
        }// end function

        public function set bitmapData(param1:BitmapData) : void
        {
            this._bitmapdata = param1;
            this.updataCutRectangle();
            return;
        }// end function

        public function draw(param1:Graphics, param2:Boolean = true, param3:Boolean = true) : void
        {
            var _loc_5:Rectangle = null;
            var _loc_6:Matrix = null;
            if (param2)
            {
                param1.clear();
            }
            var _loc_4:int = 0;
            while (_loc_4 < 9)
            {
                
                _loc_5 = this._scale9GridRect[_loc_4].rect;
                _loc_6 = this._scale9GridRect[_loc_4].matrix;
                param1.beginBitmapFill(this._bitmapdata, _loc_6, false, true);
                if (!(!param3 && _loc_4 == 4))
                {
                    param1.drawRect(_loc_5.x + this._x, _loc_5.y + this._y, _loc_5.width, _loc_5.height);
                }
                _loc_4++;
            }
            return;
        }// end function

        public function setSize(param1:int, param2:int) : void
        {
            this._width = param1;
            this._height = param2;
            this.updataCutRectangle();
            return;
        }// end function

    }
}
